import serial
import serial.tools.list_ports
import time
import pyautogui

MOUSE_STEP = int(20)
MOUSE_ACCARY = 5

def serial_port_init():
    port_list = list(serial.tools.list_ports.comports())
    port_num = len(port_list)
    if port_num == 0:
        raise Exception("No serial port")
    else:
        for i in range(0, port_num):
            print(port_list[i].name)

    for i in range(0, port_num):
        if port_num == 1:
            ser = serial.Serial(port_list[i].name, 115200, 8, 'N', 1)
            if ser.is_open:
                print(port_list[i].name+' is opened')
                return ser
            else:
                raise Exception("Open serial port failed1")
        else:
            # 对于两个以上的串口，电视机，可能有COM1,COM1口不要去打开
            if port_list[i].name == "COM1":
                print("##COM1 is not allowed to open##")
            else:
                ser = serial.Serial(port_list[i].name, 115200, 8, 'N', 1)
                if ser.is_open:
                    print(port_list[i].name + " is opened")
                    return ser
                else:
                    raise Exception("Open serial port failed2")
    return


def usb_send_kb():
    serial_port.write(bytearray(b'\xAA\x01\x00\x00\x04\x00\x00\x00\x00'))
    time.sleep(0.01)
    serial_port.write(bytearray(b'\xAA\x01\x00\x00\x00\x00\x00\x00\x00'))
    time.sleep(0.01)


def mouse_move(x_pos, y_pos):
    mouse_data = bytearray(b'\xbb\x00\x00\x00\x00')
    while True:
        x_cur, y_cur = pyautogui.position()

        x_delta = x_cur-x_pos
        if x_delta >= 0:
            if x_delta > MOUSE_STEP:
                temp = (-MOUSE_STEP).to_bytes(1, "big", signed=True)
                mouse_data[2] = temp[0]
            else:
                temp = (-x_delta).to_bytes(1, "big", signed=True)
                mouse_data[2] = temp[0]
        else:
            if x_delta < -MOUSE_STEP:
                temp = (MOUSE_STEP).to_bytes(1, "big", signed=True)
                mouse_data[2] = temp[0]
            else:
                temp = (-x_delta).to_bytes(1, "big", signed=True)
                mouse_data[2] = temp[0]

        y_delta = y_cur-y_pos
        if y_delta >= 0:
            if y_delta > MOUSE_STEP:
                temp = (-MOUSE_STEP).to_bytes(1, "big", signed=True)
                mouse_data[3] = temp[0]
            else:
                temp = (-y_delta).to_bytes(1, "big", signed=True)
                mouse_data[3] = temp[0]
        else:
            if y_delta < -MOUSE_STEP:
                temp = (MOUSE_STEP).to_bytes(1, "big", signed=True)
                mouse_data[3] = temp[0]
            else:
                temp = (-y_delta).to_bytes(1, "big", signed=True)
                mouse_data[3] = temp[0]
        if x_delta==0 and y_delta==0:
            break

        serial_port.write(mouse_data)
        time.sleep(0.01)


def mouse_left_click():
    mouse_data_down = bytearray(b'\xbb\x01\x00\x00\x00')
    mouse_data_up = bytearray(b'\xbb\x00\x00\x00\x00')
    serial_port.write(mouse_data_down)
    time.sleep(0.1)
    serial_port.write(mouse_data_up)
    time.sleep(0.1)


def mouse_left_double_click():
    mouse_left_click()
    mouse_left_click()


def mouse_right_click():
    mouse_data_down = bytearray(b'\xbb\x02\x00\x00\x00')
    mouse_data_up = bytearray(b'\xbb\x00\x00\x00\x00')
    serial_port.write(mouse_data_down)
    time.sleep(0.1)
    serial_port.write(mouse_data_up)
    time.sleep(0.1)

def mouse_right_double_click():
    mouse_right_click()
    mouse_right_click()

serial_port = serial.Serial()
if __name__ == "__main__":
    serial_port = serial_port_init()
    mouse_move(int(1920/2), int(1080/2))
    mouse_left_double_click()
